<template>
  <div class="drawerContent">
    <div class="searchContent flex_bet" v-if="type == 'recent'">
      <div>
        <span class="text">毛重时间</span>
        <el-date-picker
          v-model="searchTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleSearch"
        >
        </el-date-picker>
      </div>
      <el-input
        v-model="searchStr"
        placeholder="请输入关键字 车牌号/发货方/收货方/货品/磅单号"
        clearable
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="handleSearch"
        ></el-button>
      </el-input>
    </div>
    <div class="tableContent fl">
      <el-table v-if="type == 'error'" v-loading="loading" :data="data" border>
        <el-table-column prop="vehicle_num" label="车牌号"> </el-table-column>
        <el-table-column label="称重时间">
          <template slot-scope="scope">
            <div>
              {{
                !scope.row.time || scope.row.time == ""
                  ? ""
                  : scope.row.time.split(" ")[0]
              }}
            </div>
            <div>
              {{
                !scope.row.time || scope.row.time == ""
                  ? ""
                  : scope.row.time.split(" ")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="称重重量（吨）"></el-table-column>
      </el-table>
      <el-table
        v-else-if="type == 'recent'"
        v-loading="loading"
        :data="data"
        border
      >
        <el-table-column
          prop="vehicle_num"
          label="车牌号"
          width="120"
          fixed="left"
        >
          <template slot-scope="scope">
            <span :class="scope.row.status < -1 ? 'errorText' : ''">
              {{ scope.row.vehicle_num }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="进场时间" width="120">
          <template slot-scope="scope">
            <div>
              {{
                !scope.row.create_time || scope.row.create_time == ""
                  ? ""
                  : scope.row.create_time.split(" ")[0]
              }}
            </div>
            <div>
              {{
                !scope.row.create_time || scope.row.create_time == ""
                  ? ""
                  : scope.row.create_time.split(" ")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="离场时间" width="120">
          <template slot-scope="scope">
            <div>
              {{
                !scope.row.leave_time || scope.row.leave_time == ""
                  ? ""
                  : scope.row.leave_time.split(" ")[0]
              }}
            </div>
            <div>
              {{
                !scope.row.leave_time || scope.row.leave_time == ""
                  ? ""
                  : scope.row.leave_time.split(" ")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="vender"
          label="发货单位"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="customer"
          label="收货单位"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="payload"
          label="货物名称"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="gross_weight"
          label="毛重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="tare_weight"
          label="皮重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="net_weight"
          label="净重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="reClickPrint(scope.row)"
              >打印</el-button
            >
            <!-- </template>
          <template slot-scope="scope"> -->
            <el-button type="text" @click="reviewCar(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-tooltip
      v-if="type == 'recent'"
      class="item"
      effect="dark"
      content="记录数量不能超过500条"
      placement="top-end"
    >
      <el-button
        style="position: absolute;bottom: 30px;right: 20px;"
        type="primary"
        size="small"
        @click="showPrints"
        >批量打印</el-button
      >
    </el-tooltip>
    <hi-print
      ref="print"
      v-if="showHiPrint"
      :printDataList="hiPrintData"
      @closeHiPrint="closePrints"
    ></hi-print>
  </div>
</template>

<script>
import { getRequest, getListJson } from "@/api/public";
import hiPrint from "./hiprint.vue";
import { duration } from "moment";
export default {
  name: "table-drawer",
  props: {
    type: {
      // error-异常车辆 recent-近期出场车辆
      type: String,
      default: ""
    }
  },
  components: {
    hiPrint
  },
  data() {
    return {
      data: [],
      searchStr: "",
      searchTime: [],
      loading: false,
      total: 0,
      page: 1, //第几页
      // 批量静默打印相关数据
      showHiPrint: false,
      hiPrintData: []
    };
  },
  watch: {
    searchStr: {
      handler() {
        if (this.searchStr == "") {
          this.fetchPageData();
        }
      }
    }
  },
  computed: {},
  created() {
    this.fetchPageData();
  },
  beforeDestroy() {},
  mounted() {},
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.fetchPageData();
    },
    // 点击搜索按钮/日期选择器改变
    handleSearch() {
      this.page = 1;
      this.fetchPageData();
    },

    // 异常/近期出场车辆的详细表格（分页）
    fetchPageData() {
      this.loading = true;
      let querys = [];
      if (this.searchStr != "") {
        querys = [
          {
            property: "F_order_type",
            value: this.searchStr,
            group: "main",
            operation: "EQUAL",
            relation: "OR"
          },
          {
            property: "F_payload",
            value: this.searchStr,
            group: "main",
            relation: "OR"
          },
          {
            property: "F_vehicle_num",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_vender",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_customer",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_gross_device_num",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          }
        ];
      }
      if (this.searchTime && this.searchTime.length > 0) {
        querys = [
          ...querys,

          {
            property: "F_gross_time",
            value: this.searchTime || [],
            group: "t",
            operation: "BETWEEN",
            relation: "AND"
          }
        ];
      }

      if (this.type == "error") {
        getRequest(
          `mine/loadometer/exceptions?page=${this.page}&pageSize=10`
        ).then(response => {
          this.data = response.rows || [];
          this.loading = false;
          this.total = response.total || 0;
          this.page = response.page || 1;
        });
      } else {
        getListJson({
          alias: "loadometer_out",
          queryFilter: {
            pageBean: {
              page: this.page,
              pageSize: 10,
              showTotal: "true"
            },
            querys,
            sorter: [{ property: "leave_time", direction: "DESC" }]
          }
        }).then(response => {
          this.data = response.rows || [];
          this.loading = false;
          this.total = response.total || 0;
          this.page = response.page || 1;
        });
      }
    },
    reClickPrint(item) {
      this.$emit("reClickPrint", item);
    },
    reviewCar(item) {
      this.$emit("reviewCar", item);
    },
    // 批量静默打印相关方法
    // 点击[批量打印]
    showPrints() {
      // 请求条件筛选后的所有数据
      let querys = [];
      // let this_ = this;
      if (this.searchStr != "") {
        querys = [
          {
            property: "F_order_type",
            value: this.searchStr,
            group: "main",
            operation: "EQUAL",
            relation: "OR"
          },
          {
            property: "F_payload",
            value: this.searchStr,
            group: "main",
            relation: "OR"
          },
          {
            property: "F_vehicle_num",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_vender",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_customer",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          },
          {
            property: "F_gross_device_num",
            value: this.searchStr,
            group: "main",
            operation: "LIKE",
            relation: "OR"
          }
        ];
      }
      if (this.searchTime && this.searchTime.length > 0) {
        querys = [
          ...querys,
          {
            property: "F_gross_time",
            value: this.searchTime || [],
            group: "t",
            operation: "BETWEEN",
            relation: "AND"
          }
        ];
      }
      getListJson({
        alias: "loadometer_out",
        queryFilter: {
          pageBean: {
            page: 1,
            pageSize: 500,
            showTotal: "true"
          },
          querys,
          sorter: [{ property: "leave_time", direction: "DESC" }]
        }
      })
        .then(response => {
          this.hiPrintData = response.rows || [];
          let hiPrintTotal = response.total || 0;
          if (hiPrintTotal == 0) {
            // 等于0 则弹出提示框[没有符合筛选条件的记录！]

            this.$notify.error({
              title: "没有符合筛选条件的记录",
              message: "请确认筛选范围！",
              position: "bottom-right",
              offset: 100,
              showClose: false
            });
          } else if (hiPrintTotal > 500) {
            // 数据条数大于500 则弹出提示框[共有X条记录，批量打印总数不能超过500条，请缩小筛选范围！]
            this.$notify.error({
              title: "批量打印记录不能超过500条",
              message: `共有${hiPrintTotal}条记录，请缩小筛选范围！`,
              position: "bottom-right",
              offset: 100,
              showClose: false
            });
          } else {
            // 数据条数大于0小于等于500
            // 弹出确认框 是否确认打印x条数据？

            // this.$confirm("是否确认打印" + hiPrintTotal + "条数据？", "警告", {
            //   confirmButtonText: "确定",
            //   cancelButtonText: "取消",
            //   type: "warning",
            //   modal: "false"
            // })
            //   .then(() => {
            //     this.showHiPrint = true;
            //   })
            //   .catch(() => {
            //     //
            //   });
            this.showHiPrint = true;
          }
        })
        .catch(error => {
          this.$notify.error({
            title: "请稍后再试",
            position: "bottom-right",
            offset: 100,
            showClose: false
          });
        });
    },
    closePrints() {
      this.showHiPrint = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.drawerContent {
  height: 100%;
  width: 100%;
  position: relative;
  .searchContent {
    padding: 0 20px;
    margin-bottom: 10px;
    width: 100%;
    line-height: 40px;
    align-items: center;
    .text {
      font-size: 18px;
    }
    .el-input {
      width: 350px;
      height: 40px;
    }
  }
  .tableContent {
    height: calc(100% - 60px);
    width: 100%;
    padding: 0 20px;
    overflow-y: auto;
    .el-table {
      font-size: 18px;
      margin: 10px 0;
    }
  }
  // .errorText {
  //   color: #ff5151;
  // }
  .errorText {
    position: relative;

    &::after {
      content: "!";
      position: absolute;
      right: -20px;
      top: 0px;
      display: inline-block;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      text-align: center;
      line-height: 14px;
      font-size: 12px;
      font-weight: bold;
      background: #fa5858;
      color: #fff;
      border: 1px solid #fff;
      // box-shadow: 0px 0px 3px 0px #000000;
    }
  }
}
::v-deep {
  input {
    height: 40px;
    padding-left: 0px;
    padding-right: 10px !important;
  }
  .cell.el-tooltip {
    white-space: nowrap;
  }
  .el-table__cell {
    padding: 10px 0;
  }
}
</style>
